//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 9
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using ProxyHelpers.EWS;

namespace ChangeRecurrenceOfARecurringSeries
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            // Note that you will nee the ItemId of a recurring master before you call the 
            // ChangeRecurrenceOfARecurringSeries method below.
        }

        /// <summary>
        /// Change the recurrence of a recurring series by updating the
        /// Recurrence property on a recurring master.  Call will tell
        /// EWS to auto-resolve any conflicts and send updates to any
        /// and all attendees (if the recurring series is also
        /// a recurring meeting) (Listing 9-44)
        /// </summary>
        /// <param name="binding">Binding to use for the call</param>
        /// <param name="idOfRecurringMaster">id of the recurring master</param>
        /// <param name="newRecurrenceInformation">new recurrence information</param>
        /// <returns>Updated id of the recurring master</returns>
        ///
        public static ItemIdType ChangeRecurrenceOfARecurringSeries(
            ExchangeServiceBinding binding,
            ItemIdType idOfRecurringMaster,
            RecurrenceType newRecurrenceInformation)
        {
            // Verify that the newRecurrenceInformation is complete
            //
            if ((newRecurrenceInformation.Item == null) ||
                (newRecurrenceInformation.Item1 == null))
            {
                throw new ArgumentException("Both the pattern and range of " +
                    "the new recurrence must be specified.");
            }

            // Create an empty CalendarItem type to hold the recurrence for the
            // update item call
            //
            CalendarItemType calitem1 = new CalendarItemType();
            calitem1.Recurrence = newRecurrenceInformation;

            // Make the updated recurrence part of a SetItemFieldType instance by 
            // specifying both the field we are updating (calendar:Recurrence) and
            // providing our temp calendar item with the recurrence property set.
            //
            SetItemFieldType itemUpdateDescription = new SetItemFieldType();
            itemUpdateDescription.Item = new PathToUnindexedFieldType();

            ((PathToUnindexedFieldType)itemUpdateDescription.Item).FieldURI =
                UnindexedFieldURIType.calendarRecurrence;
            itemUpdateDescription.Item1 = calitem1;

            // Create an UpdateItem requests
            //
            UpdateItemType updateItemRequest = new UpdateItemType();
            updateItemRequest.ConflictResolution = ConflictResolutionType.AutoResolve;
            updateItemRequest.SendMeetingInvitationsOrCancellations =
                CalendarItemUpdateOperationType.SendOnlyToAll;
            updateItemRequest.SendMeetingInvitationsOrCancellationsSpecified = true;
            updateItemRequest.ItemChanges = new ItemChangeType[] {
        new ItemChangeType() };

            // Set the id of the UpdateItem request to be the id of the
            // recurring master
            //
            updateItemRequest.ItemChanges[0].Item = idOfRecurringMaster;
            updateItemRequest.ItemChanges[0].Updates = new SetItemFieldType[] {
        itemUpdateDescription };

            // Call the UpdateItem web method via the Exchange Serivce Binding
            UpdateItemResponseType updateItemResponse =
                binding.UpdateItem(updateItemRequest);

            // Verify that the UpdateItem request was successful
            if (updateItemResponse.ResponseMessages.Items[0].ResponseClass !=
                ResponseClassType.Success)
            {
                // Indicate that we have a problem
                throw new Exception(String.Format(
                    "Unable to update recurring master\r\n{0}\r\n{1}",
                    updateItemResponse.ResponseMessages.Items[0].ResponseCode,
                    updateItemResponse.ResponseMessages.Items[0].MessageText));
            }

            // Success, get the new id of the recurring master
            ItemInfoResponseMessageType itemInfoResponseMessage =
                (ItemInfoResponseMessageType)updateItemResponse.
                    ResponseMessages.Items[0];

            return itemInfoResponseMessage.Items.Items[0].ItemId;
        }

    }
}
